package net.siufung.market.base.dict.controller.regulation;

import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import net.siufung.core.exception.BizServiceException;
import net.siufung.core.model.VerifyRepeatDto;
import net.siufung.core.model.enums.MethodEventEnum;
import net.siufung.core.model.regulation.AbstractRegulation;
import net.siufung.market.base.dict.controller.model.dict.DictSaveDto;
import net.siufung.market.base.dict.controller.model.dict.DictUpdateDto;
import net.siufung.market.base.dict.mapper.IDictMapper;
import net.siufung.market.base.dict.mapper.entity.Dict;

import java.util.Map;
import java.util.Objects;

/**
 * @author 陈建峰
 * @date 2023/8/5 10:00 PM
 * @since V1.0.0
 */
public class DictRegulation {

    @AllArgsConstructor
    public static class Save extends AbstractRegulation<DictSaveDto> {

        private static final long serialVersionUID = 1396909626312148157L;

        private final IDictMapper dictMapper;

        @Override
        public boolean isSatisfiedBy(DictSaveDto dictSaveDto) {
            VerifyRepeatDto<Integer> verifyRepeatDto = new VerifyRepeatDto<>();
            verifyRepeatDto.setMethodEventEnum(MethodEventEnum.ADD);
            verifyRepeatDto.setPk(null);
            if(!Strings.isNullOrEmpty(dictSaveDto.getDictCode())){
                verifyRepeatDto.setVerifyKeyValue(dictSaveDto.getDictCode());
                if(!dictMapper.checkDictCodeNotRepeat(verifyRepeatDto)){
                    throw new BizServiceException("字典编码重复！");
                }
            }
            verifyRepeatDto.setVerifyKeyValue(dictSaveDto.getDictValue());
            Map<String, Object> extra = Maps.newHashMap();
            extra.put("dictTypeId", dictSaveDto.getDictTypeId());
            verifyRepeatDto.setExtra(extra);
            if(!dictMapper.checkValueWithDictTypeNotRepeat(verifyRepeatDto)){
                throw new BizServiceException("统一字典类型值不能重复！");
            }
            return Boolean.TRUE;
        }
    }

    @AllArgsConstructor
    public static class Update extends AbstractRegulation<DictUpdateDto> {

        private static final long serialVersionUID = 1396909626312148157L;

        private final IDictMapper dictMapper;

        @Override
        public boolean isSatisfiedBy(DictUpdateDto dictUpdateDto) {
            VerifyRepeatDto<Integer> verifyRepeatDto = new VerifyRepeatDto<>();
            verifyRepeatDto.setMethodEventEnum(MethodEventEnum.UPDATE);
            verifyRepeatDto.setPk(dictUpdateDto.getId());
            verifyRepeatDto.setVerifyKeyValue(dictUpdateDto.getDictValue());
            Map<String, Object> extra = Maps.newHashMap();
            extra.put("dictTypeId", dictUpdateDto.getDictTypeId());
            verifyRepeatDto.setExtra(extra);
            if(!dictMapper.checkValueWithDictTypeNotRepeat(verifyRepeatDto)){
                throw new BizServiceException("统一字典类型值不能重复！");
            }
            return Boolean.TRUE;
        }
    }

    @AllArgsConstructor
    public static class Remove extends AbstractRegulation<Integer> {

        private static final long serialVersionUID = -2874308489110744103L;

        private final IDictMapper dictMapper;

        @Override
        public boolean isSatisfiedBy(Integer dictId) {
            if(Objects.isNull(dictId)){
                throw new BizServiceException("字典主键不能为空！");
            }
            Dict dict = dictMapper.selectById(dictId);
            if(Objects.isNull(dict)){
                throw new BizServiceException("字典不存在！");
            }
            if(dict.getDefaultFlag()){
                throw new BizServiceException("默认字典不能删除！");
            }
            return Boolean.TRUE;
        }
    }

    @AllArgsConstructor
    public static class Status extends AbstractRegulation<Integer> {

        private static final long serialVersionUID = -2874308489110744103L;

        private final IDictMapper dictMapper;

        @Override
        public boolean isSatisfiedBy(Integer dictId) {
            if(Objects.isNull(dictId)){
                throw new BizServiceException("字典主键不能为空！");
            }
            Dict dict = dictMapper.selectById(dictId);
            if(Objects.isNull(dict)){
                throw new BizServiceException("字典不存在！");
            }
            return Boolean.TRUE;
        }
    }
}
